package com.tgy.MyFlowable.workflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctsi.core.common.enums.StatusType;
import com.ctsi.core.common.exception.MarsException;
import com.ctsi.core.common.util.$;
import com.ctsi.core.database.util.PageUtil;
import com.ctsi.workflow.dto.FlwCategoryDTO;
import com.ctsi.workflow.entity.FlwCategory;
import com.ctsi.workflow.enums.FlwCategoryExceptionEnum;
import com.ctsi.workflow.mapper.FlwCategoryMapper;
import com.ctsi.workflow.service.IFlwCategoryService;
import org.springframework.stereotype.Service;

/**
 * 流程分类表 服务实现类
 *
 * @version 1.0
 * @author: wang xiao xiang
 * @date: 2021/8/8 17:00
 */
@Service
public class FlwCategoryServiceImpl extends ServiceImpl<FlwCategoryMapper, FlwCategory> implements IFlwCategoryService {

    @Override
    public IPage<FlwCategory> listPage(FlwCategoryDTO flwCategoryDTO) {
        LambdaQueryWrapper<FlwCategory> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like($.isNoneBlank(flwCategoryDTO.getName()), FlwCategory::getName, flwCategoryDTO.getName());
        queryWrapper.like($.isNoneBlank(flwCategoryDTO.getCode()), FlwCategory::getCode, flwCategoryDTO.getCode());
        queryWrapper.orderByAsc(FlwCategory::getSort);
        return this.baseMapper.selectPage(PageUtil.getPage(flwCategoryDTO), queryWrapper);
    }

    @Override
    public String getNameByCode(String code) {
        LambdaQueryWrapper queryWrapper =
                Wrappers.<FlwCategory>lambdaQuery()
                        .eq(FlwCategory::getCode, code)
                        .eq(FlwCategory::getStatus, StatusType.ENABLE.getCode());
        FlwCategory flwCategory = this.getOne(queryWrapper);

        // 如果code不存在，则抛出异常
        if ($.isEmpty(flwCategory)) {
            throw new MarsException(FlwCategoryExceptionEnum.CATEGORY_NOT_EXIST);
        }
        return flwCategory.getName();
    }
}
